from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
#chromedriver版本为137版本
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()
def add_user(browser, user_data):
    # 点击菜单进入用户页面
    browser.find_element(By.CSS_SELECTOR, ".menu_titre > a[title='用户和组']").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_user_card > a[title='新建用户']").click()
    # 称谓
    browser.find_element(By.CSS_SELECTOR, f"select#civility_code > option[value='{user_data['civility']}']").click()
    # 姓氏
    browser.find_element(By.ID, "lastname").send_keys(user_data['lastname'])
    # 名字
    browser.find_element(By.ID, "firstname").send_keys(user_data['firstname'])
    # 登录名
    browser.find_element(By.ID, "login").clear()
    browser.find_element(By.ID, "login").send_keys(user_data['login'])
    # 系统管理员
    browser.find_element(By.CSS_SELECTOR, f"select#admin > option[value='{user_data['admin']}']").click()
    # 性别
    browser.find_element(By.CSS_SELECTOR, f"select#gender > option[value='{user_data['gender']}']").click()
    # 员工
    browser.find_element(By.CSS_SELECTOR, ".centpercent tr:nth-child(7)>td:nth-child(2)>input").click()
    # 超级管理员
    browser.find_element(By.CSS_SELECTOR, f"select#fk_user > option[value='{user_data['fk_user']}']").click()
    # 有效日期
    browser.find_element(By.ID, "datestartvalidity").send_keys(user_data['start_validity'])
    browser.find_element(By.ID, "dateendvalidity").send_keys(user_data['end_validity'])
    # 密码
    browser.find_element(By.ID, "generate_password").click()
    # 地址
    browser.find_element(By.ID, "address").send_keys(user_data['address'])
    # 邮政编码
    browser.find_element(By.ID, "zipcode").send_keys(user_data['zipcode'])
    # 城市
    browser.find_element(By.ID, "town").send_keys(user_data['town'])
    # 国家
    browser.find_element(By.CSS_SELECTOR, f"select#selectcountry_id > option[value='{user_data['country']}']").click()
    # 省份
    browser.find_element(By.CSS_SELECTOR, f"select#state_id > option[value='{user_data['state']}']").click()
    # 电话等联系方式
    browser.find_element(By.NAME, "office_phone").send_keys(user_data['office_phone'])
    browser.find_element(By.NAME, "user_mobile").send_keys(user_data['mobile'])
    browser.find_element(By.NAME, "office_fax").send_keys(user_data['fax'])
    browser.find_element(By.NAME, "email").send_keys(user_data['email'])
    # 签名与备注
    browser.find_element(By.ID, "signature").send_keys(user_data['signature'])
    browser.find_element(By.ID, "note_public").send_keys(user_data['note_public'])
    browser.find_element(By.ID, "note_private").send_keys(user_data['note_private'])
    # 职位与工时
    browser.find_element(By.NAME, "job").send_keys(user_data['job'])
    browser.find_element(By.NAME, "weeklyhours").send_keys(user_data['weeklyhours'])
    # 雇佣信息
    browser.find_element(By.ID, "dateemployment").send_keys(user_data['employment_start'])
    browser.find_element(By.ID, "dateemploymentend").send_keys(user_data['employment_end'])
    # 生日
    browser.find_element(By.ID, "dateofbirth").send_keys(user_data['birthday'])
    # 提交表单
    browser.find_element(By.NAME, "save").click()
# 测试用例数据
test_users = [
    {
        "civility": "MR",
        "lastname": "Wang",
        "firstname": "Mei",
        "login": "WM01",
        "admin": "0",
        "gender": "woman",
        "fk_user": "-1",
        "start_validity": "2025/06/15",
        "end_validity": "2025/11/30",
        "address": "上海市浦东新区张江高科技园区",
        "zipcode": "201203",
        "town": "上海",
        "country": "9",
        "state": "61",
        "office_phone": "021-55558888",
        "mobile": "13812345678",
        "fax": "021-55559999",
        "email": "mei.wang@example.com",
        "signature": "王梅",
        "note_public": "市场部经理",
        "note_private": "2025年Q3重点项目负责人",
        "job": "Marketing Manager",
        "weeklyhours": "40",
        "employment_start": "2025/06/01",
        "employment_end": "2026/06/01",
        "birthday": "1990/08/15"
    },
    {
        "civility": "MR",
        "lastname": "Zhang",
        "firstname": "San",
        "login": "ZS01",
        "admin": "1",
        "gender": "man",
        "fk_user": "1",
        "start_validity": "2025/06/01",
        "end_validity": "2026/05/31",
        "address": "北京市海淀区中关村南大街5号",
        "zipcode": "100081",
        "town": "北京",
        "country": "9",
        "state": "62",
        "office_phone": "010-88887777",
        "mobile": "13698765432",
        "fax": "010-88886666",
        "email": "san.zhang@example.com",
        "signature": "张三",
        "note_public": "系统管理员",
        "note_private": "负责核心系统维护",
        "job": "System Administrator",
        "weeklyhours": "45",
        "employment_start": "2025/05/15",
        "employment_end": "2026/05/15",
        "birthday": "1985/11/22"
    },
    {
        "civility": "MLE",
        "lastname": "Chen",
        "firstname": "Xiao",
        "login": "CX01",
        "admin": "0",
        "gender": "woman",
        "fk_user": "1",
        "start_validity": "2025/06/20",
        "end_validity": "2026/01/31",
        "address": "广州市天河区天河路228号",
        "zipcode": "510620",
        "town": "广州",
        "country": "9",
        "state": "63",
        "office_phone": "020-77778888",
        "mobile": "13789012345",
        "fax": "020-77779999",
        "email": "xiao.chen@example.com",
        "signature": "陈晓",
        "note_public": "实习生",
        "note_private": "人力资源部实习生",
        "job": "HR Intern",
        "weeklyhours": "30",
        "employment_start": "2025/06/10",
        "employment_end": "2025/09/10",
        "birthday": "1998/03/05"
    },
    {
        "civility": "MR",
        "lastname": "Liu",
        "firstname": "Wei",
        "login": "LW01",
        "admin": "0",
        "gender": "man",
        "fk_user": "1",
        "start_validity": "2025/06/18",
        "end_validity": "2026/06/17",
        "address": "深圳市南山区科技园",
        "zipcode": "518057",
        "town": "深圳",
        "country": "9",
        "state": "64",
        "office_phone": "0755-88886666",
        "mobile": "13923456789",
        "fax": "0755-88887777",
        "email": "wei.liu@example.com",
        "signature": "刘伟",
        "note_public": "软件开发工程师",
        "note_private": "参与电商平台开发",
        "job": "Software Developer",
        "weeklyhours": "40",
        "employment_start": "2025/06/01",
        "employment_end": "2026/06/01",
        "birthday": "1992/07/18"
    },
    {
        "civility": "MR",
        "lastname": "Zhao",
        "firstname": "Jing",
        "login": "ZJ01",
        "admin": "0",
        "gender": "woman",
        "fk_user": "-1",
        "start_validity": "2025/06/25",
        "end_validity": "2025/12/24",
        "address": "杭州市西湖区文三路",
        "zipcode": "310012",
        "town": "杭州",
        "country": "9",
        "state": "65",
        "office_phone": "0571-55556666",
        "mobile": "13567890123",
        "fax": "0571-55557777",
        "email": "jing.zhao@example.com",
        "signature": "赵静",
        "note_public": "财务分析师",
        "note_private": "负责月度财务报告",
        "job": "Financial Analyst",
        "weeklyhours": "38",
        "employment_start": "2025/06/15",
        "employment_end": "2026/06/15",
        "birthday": "1987/12/10"
    }
]

if __name__ == '__main__':
    try:
        login()
        for user in test_users:
            add_user(browser,user)
            sleep(1)
    except Exception as e:
        print(e)
    finally:
        sleep(1)
        browser.quit()